package com.turing.zuul.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class PostFilter extends ZuulFilter {

    private Log logger = LogFactory.getLog(PostFilter.class);

    @Override
    public String filterType() {
        /*
        pre：可以在请求被路由之前调用
        route：在路由请求时候被调用
        post：在route和error过滤器之后被调用
        error：处理请求时发生错误时被调用
        * */
        // 前置过滤器
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        //// 优先级为0，数字越大，优先级越低
        return 2;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        //过滤各种POST请求
//        logger.info( "in post filter, ctx is " + ctx );
        if (request.getMethod().equals(RequestMethod.OPTIONS.name())) {
            return false;
        }

        return true;
    }

    @Override
    public Object run() {
//        logger.info("*****************PostFilter run start*****************");
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletResponse response = ctx.getResponse();
        HttpServletRequest request = ctx.getRequest();
        if( request.getRequestURI().contains("getMarkPdfZip") || request.getRequestURI().contains("getFileCompareResultNew")
                || request.getRequestURI().contains("exportCompareResult") || request.getRequestURI().contains("exportFile")
                || request.getRequestURI().contains("expRefTable") || request.getRequestURI().contains("getViewPdf")
                || request.getRequestURI().contains("getImage")){
            // 响应标头指定 指定可以访问资源的URI路径
            //response.setHeader("Access-Control-Allow-Origin", "*");

            //响应标头指定响应访问所述资源到时允许的一种或多种方法
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");

            //设置 缓存可以生存的最大秒数
            response.setHeader("Access-Control-Max-Age", "3600");

            //设置  受支持请求标头
            response.setHeader("Access-Control-Allow-Headers", "*");

            // 指示的请求的响应是否可以暴露于该页面。当true值返回时它可以被暴露
            response.setHeader("Access-Control-Allow-Credentials","true");
            return null;
        }
        response.setHeader("token", request.getHeader("token"));
        response.setHeader("Authorization", request.getHeader("Authorization"));
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Expose-Headers", "X-forwared-port, X-forwarded-host,Content-Disposition");
        response.setHeader("Vary", "Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token, myAuth");
        //允许继续路由
        ctx.setSendZuulResponse(true);
//        ctx.setResponseStatusCode(200);
//        logger.info("*****************PostFilter run end*****************");
        return null;
    }
}
/**
 * @author ：XiLuo
 * @date ：Created in 2019/5/22 22:32
 * @description：
 * @modified By：
 * @version: $
 */